package com.compuvest.table;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@WebServlet("/table/advertable")
public class advertTable extends HttpServlet {

    public static Logger log = LoggerFactory.getLogger(advertTable.class);

    private TableMySQL mysql;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        mysql = new TableMySQL();

        log.info("Init()");
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/xml;charset=UTF-8");

        //boolean needTotalRow = request.getParameter("get_total") != null ? true : false;
        String tableID = request.getParameter("id");
        String pageSize = request.getParameter("page_size");
        //int tableOffset = 0; try { tableOffset = request.getParameter("offset") == null ? 0 : Integer.parseInt(request.getParameter("offset")); } catch(Exception ignore) { }
        int distinct = 0; try { distinct = request.getParameter("distinct") == null ? 0 : Integer.parseInt(request.getParameter("distinct")); } catch(Exception ignore) { }

        StringBuilder xml = new StringBuilder(500);
                xml.append("<?xml version='1.0' encoding='UTF-8'?>");
                xml.append("<ajax-response>");

                if(distinct == 0) {
                    xml.append("<response type='object' id='").append(tableID).append("_updater'>");

/*                    xml.append("<rows update_ui='true' offset='").append(tableOffset).append("'>");
                    xml.append("<tr><td>accwareCode1</td><td>mfrName</td><td>mfrNumber</td><td>upc</td><td>name</td><td>conditions</td><td>costPrice</td><td>sellPrice</td></tr>");
                    xml.append("<tr><td>accwareCode2</td><td>mfrName</td><td>mfrNumber</td><td>upc</td><td>name</td><td>conditions</td><td>costPrice</td><td>sellPrice</td></tr>");
                    xml.append("<tr><td>accwareCode3</td><td>mfrName</td><td>mfrNumber</td><td>upc</td><td>name</td><td>conditions</td><td>costPrice</td><td>sellPrice</td></tr>");
                    xml.append("<tr><td>accwareCode4</td><td>mfrName</td><td>mfrNumber</td><td>upc</td><td>name</td><td>conditions</td><td>costPrice</td><td>sellPrice</td></tr>");
                    xml.append("<tr><td>accwareCode5</td><td>mfrName</td><td>mfrNumber</td><td>upc</td><td>name</td><td>conditions</td><td>costPrice</td><td>sellPrice</td></tr>");
                    xml.append("<tr><td>accwareCode6</td><td>mfrName</td><td>mfrNumber</td><td>upc</td><td>name</td><td>conditions</td><td>costPrice</td><td>sellPrice</td></tr>");
                    xml.append("</rows>");*/

                    xml.append(mysql.getAdvertTable(request));
                    xml.append("</response>");
                }
/*
                else if(distinct == 4) {
                    xml.append("<response type='object' id='ex3_updater'>");
                    xml.append("<rows update_ui='true' offset='0' distinct='4'>");
                    xml.append("<tr><td>France</td></tr>");
                    xml.append("<tr><td>Germany</td></tr>");
                    xml.append("<tr><td>Brazil</td></tr>");
                    xml.append("<tr><td>Belgium</td></tr>");
                    xml.append("<tr><td>Switzerland</td></tr>");
                    xml.append("<tr><td>Venezuela</td></tr>");
                    xml.append("<tr><td>Austria</td></tr>");
                    xml.append("<tr><td>Mexico</td></tr>");
                    xml.append("</rows>");
                    xml.append("</response>");
                }
                else if(distinct == 7) {
                    xml.append("<response type='object' id='ex3_updater'>");
                    xml.append("<rows update_ui='true' offset='0' distinct='7'>");
                    xml.append("<tr><td>1996</td></tr>");
                    xml.append("<tr><td>1997</td></tr>");
                    xml.append("<tr><td>1998</td></tr>");
                    xml.append("</rows>");
                    xml.append("</response>");
                }
                else if(distinct == 8) {
                    xml.append("<response type='object' id='ex3_updater'>");
                    xml.append("<rows update_ui='true' offset='0' distinct='8'>");
                    xml.append("<tr><td>1996</td></tr>");
                    xml.append("<tr><td>1997</td></tr>");
                    xml.append("<tr><td>1998</td></tr>");
                    xml.append("</rows>");
                    xml.append("</response>");
                }
*/
//                xml.append("<debug>").append(mysql.getVersion()).append("</debug>");
                xml.append("</ajax-response>");

        try(PrintWriter out = response.getWriter()) {
                out.print(xml.toString());
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "advertTable";
    }// </editor-fold>

}
